﻿using AntDesign;
using SageKing.Database.EF.Entity.Contracts;


namespace SageKing.Blazor.OpenIdService.Components.Shared
{
    public class TableSetModal<T> : ModalBase<T>
    where T : ICloneable
    {
        /// <summary>
        /// 列名
        /// </summary>
        public Dictionary<string, TemplateConfigBase> TableColumnsNames { get; set; }


        public double tableHeight
        {
            get => field; // 'field' 关键字代表编译器生成的幕后字段
            set
            {
                if (field != value)
                {
                    field = value > 0 ? value : 350;
                    //变更
                    ModalTemplate = GetModalTemplate();
                    StateHasChangedAction?.Invoke();
                }
            }
        }
        public double offsetHeight = 60 * 4 - 5;
        public TableSetModal(AComponentBase from, Dictionary<string, TemplateConfigBase> tableColumnsNames, double height = 845)
        {
            this.StateHasChangedAction = from.StateHasChanged;

            this.TableColumnsNames = tableColumnsNames;

            this.tableHeight = height;

            Title = $"表格设置->{from.TemplateName}";
            Width = "50%";
            MaxBodyHeight = "90vh";
            Style = "margin-bottom:50px; padding: 1px;margin-top: 0px; padding-top: 0px; min-height: 50vh; overflow-y:auto;";

        }

        public override Task ResizedChanged(HtmlElement e)
        {
            this.tableHeight = e.ClientHeight.ParseToDouble(845) - offsetHeight;
            return Task.CompletedTask;
        }

        public RenderFragment GetModalTemplate() => builder =>
        {
            var seq = 0;
            var t = this.GetType();
            builder.OpenComponent<TableSet>(seq++);
            builder.AddAttribute(seq++, "tableHeightCssName", $"TableSetModal{t.Name}");
            builder.AddAttribute(seq++, "TableColumnsNames", TableColumnsNames);
            builder.AddAttribute(seq++, "tableHeight", tableHeight);
            builder.CloseComponent();

        };
    }
}
